﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Net6Shell.Common;
using Net6Shell.Controllers;
using Net6Shell.DataModel;
using Net6Shell.Dto;
using Net6Shell.DTO;
using Net6Shell.IRepository;
using Net6Shell.Vo;
using SqlSugar;
using System.Data;
using static Azure.Core.HttpHeader;

namespace MyApp.WebApi.Controllers
{
    /// <summary>
    /// 接口工具控制器
    /// </summary>
    public class InterfaceToolController : NoAuthApiControllerBase
    {
        IInterfaceToolRepository service;
        IMapper mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="mapper"></param>
        public InterfaceToolController(IInterfaceToolRepository repository, IMapper mapper)
        {
            this.service = repository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 通过ID获取接口工具记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public InterfaceToolEntityDTO GetById(string id)
        {
            NLogHelper.GetLog().Info("测试一条日志");
            var data = service.GetById(id);
            var res = mapper.Map<InterfaceToolEntityDTO>(data);
            return res;
        }

        /// <summary>
        /// sql获取datatable
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        [HttpGet]
        public SwaggerDataTable GetDataTable(string sql)
        {
            DataTable dataTable = service.GetDataTable(sql); 
            SwaggerDataTable swaggerDataTable = new SwaggerDataTable(dataTable);
            return swaggerDataTable;            
        }

        /// <summary>
        /// 通过编码获取一条记录
        /// </summary>
        /// <param name="interfaceCode">接口编码</param>
        /// <returns></returns>
        [HttpGet]
        public InterfaceToolEntity GetInterfaceToolByCode(string interfaceCode)
        {
            Expressionable<InterfaceToolEntity> exp = Expressionable.Create<InterfaceToolEntity>();
            exp.And(it => it.INTERFACECODE == interfaceCode);
            return service.GetListByWhere(exp)?.FirstOrDefault();
        }

        /// <summary>
        ///  批量保存接口工具记录
        /// </summary>
        /// <param name="interfaceToolList"></param>
        /// <returns></returns>
        [HttpPost]
        public CommonResult SaveInterfaceTool([FromBody] List<InterfaceToolEntity> interfaceToolList)
        {
            var result = service.SaveInterfaceTool(interfaceToolList);
            return result;
        }

        /// <summary>
        ///分页查询获取接口工具列表 
        /// </summary>
        /// <param name="pageVm"></param>
        /// <returns></returns>
        [HttpPost]
        public CommonResult GetInterfacePageList([FromBody] PageVm<QueryInterFace> pageVm)
        {
            var result = service.GetInterfacePageList(pageVm);
            return new CommonResult() { ReturnData = result };
        }

    }
}
